package edu.upn.sigemfi.rec.bc;

import edu.upn.sigemfi.rec.be.Cartera;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class CarteraDelegate {

    private CarteraFacadeLocal local;

    public CarteraDelegate() {
        local = lookupCarteraFacade();
    }

    private CarteraFacadeLocal lookupCarteraFacade() {
        try {
            Context c = new InitialContext();
            return (CarteraFacadeLocal) c.lookup("java:comp/env/CarteraFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    public void crear(Cartera cartera) throws Exception {
        try {
            local.create(cartera);
        } catch (Exception ex) {
            local = lookupCarteraFacade();
            throw new Exception("Crear Cartera" + ex.getMessage());
        }
    }

    public void editar(Cartera cartera) throws Exception {
        try {
            local.edit(cartera);
        } catch (Exception ex) {
            local = lookupCarteraFacade();
            throw new Exception("Editar Cartera" + ex.getMessage());
        }
    }

    public void eliminar(Cartera cartera) throws Exception {
        try {
            local.remove(cartera);
        } catch (Exception ex) {
            local = lookupCarteraFacade();
            throw new Exception("Crear Cartera" + ex.getMessage());
        }
    }

    public List<Cartera> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupCarteraFacade();
            throw new Exception("Listar Carteras. " + e.getMessage(), e);
        }
    }
    

    public Cartera buscar(Long codigo) throws Exception {
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local = lookupCarteraFacade();
            throw new Exception("Buscar Cartera. " + e.getMessage(), e);
        }
    }
}
